package yt.demo.demoadmin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import yt.demo.appcommonbean.http.DeleteRequest;
import yt.demo.appcommonbean.http.RestResponse;
import yt.demo.appcommondb.base.BaseEntity;
import yt.demo.appcommondb.dto.SplitPageDTO;
import yt.demo.appcommondb.vo.PageVO;
import yt.demo.appcoresystem.entity.SystemDictData;
import yt.demo.appcoresystem.entity.SystemDictType;
import yt.demo.appcoresystem.service.ISystemDictDataService;
import yt.demo.appcoresystem.service.ISystemDictTypeService;

import java.util.List;

@RestController
@RequestMapping("/dict")
public class DictController extends BaseController {

    private final ISystemDictTypeService typeService;

    private final ISystemDictDataService dataService;

    @Autowired
    public DictController(ISystemDictTypeService typeService, ISystemDictDataService dataService) {
        this.typeService = typeService;
        this.dataService = dataService;
    }

    @GetMapping("/getTypePageList")
    public RestResponse<PageVO<SystemDictType>> getTypePageList(SplitPageDTO page, SystemDictType params) {
        return RestResponse.ok(this.typeService.getPageList(page, params, w -> w.orderByDesc(SystemDictType::getId), null));
    }

    @PostMapping("/typeCreate")
    public RestResponse<SystemDictType> typeCreate(@RequestBody @Validated(BaseEntity.CreateGroup.class) SystemDictType req) {
        return RestResponse.ok(this.typeService.create(req));
    }

    @PostMapping("/typeUpdate")
    public RestResponse<SystemDictType> typeUpdate(@RequestBody @Validated(BaseEntity.UpdateGroup.class) SystemDictType req) {
        return RestResponse.ok(this.typeService.update(req));
    }

    @PostMapping("/typeReset")
    public RestResponse<SystemDictType> typeReset(@RequestBody SystemDictType req) {
        return RestResponse.ok(this.typeService.update(req));
    }

    @PostMapping("/typeDelete")
    public RestResponse<Void> typeDelete(@RequestBody DeleteRequest req) {
        this.typeService.delete(req);
        return RestResponse.ok();
    }

    @GetMapping("/getList")
    public RestResponse<List<SystemDictData>> getList(SystemDictData params) {
        return RestResponse.ok(this.dataService.getList(params));
    }

    @GetMapping("/getDataPageList")
    public RestResponse<PageVO<SystemDictData>> getDataPageList(SplitPageDTO page, SystemDictData params) {
        return RestResponse.ok(this.dataService.getPageList(page, params, w -> w.orderByDesc(SystemDictData::getId), null));
    }

    @PostMapping("/dataCreate")
    public RestResponse<SystemDictData> dataCreate(@RequestBody @Validated(BaseEntity.CreateGroup.class) SystemDictData req) {
        return RestResponse.ok(this.dataService.cCreate(req));
    }

    @PostMapping("/dataUpdate")
    public RestResponse<SystemDictData> dataUpdate(@RequestBody @Validated(BaseEntity.UpdateGroup.class) SystemDictData req) {
        return RestResponse.ok(this.dataService.cUpdate(req));
    }

    @PostMapping("/dataReset")
    public RestResponse<SystemDictData> dataReset(@RequestBody SystemDictData req) {
        return RestResponse.ok(this.dataService.cUpdate(req));
    }

    @PostMapping("/dataDelete")
    public RestResponse<Void> dataDelete(@RequestBody DeleteRequest req) {
        this.dataService.cDelete(req);
        return RestResponse.ok();
    }

}
